using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
    public partial class Tbl004_Comment : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_coStatus;
			private SqlInt32		_coID;
			private SqlString		_nID, _nIDOld, _coEmail, _coName, _coContent;
		#endregion


		public Tbl004_Comment()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CoID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _coID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coEmail));
				int	length = 0;
				if(!_coContent.IsNull)
				{
					length = _coContent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@CoContent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coContent));
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coStatus));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CoID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _coID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coEmail));
				int	length = 0;
				if(!_coContent.IsNull)
				{
					length = _coContent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@CoContent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coContent));
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CoStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _coStatus));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWNIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_UpdateAllWNIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _nID));
				cmdToExecute.Parameters.Add(new SqlParameter("@NIDOld", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::UpdateAllWNIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CoID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _coID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl004_Comment");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CoID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _coID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_coID = (Int32)toReturn.Rows[0]["CoID"];
					_coName = toReturn.Rows[0]["CoName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CoName"];
					_coEmail = toReturn.Rows[0]["CoEmail"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CoEmail"];
					_coContent = toReturn.Rows[0]["CoContent"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CoContent"];
					_nID = toReturn.Rows[0]["NID"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["NID"];
					_coStatus = toReturn.Rows[0]["CoStatus"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["CoStatus"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl004_Comment");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWNIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl004_Comment_SelectAllWNIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl004_Comment");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl004_Comment::SelectAllWNIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlInt32 CoID
		{
			get
			{
				return _coID;
			}
			set
			{
				SqlInt32 coIDTmp = (SqlInt32)value;
				if(coIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("CoID", "CoID can't be NULL");
				}
				_coID = value;
			}
		}


		public SqlString CoName
		{
			get
			{
				return _coName;
			}
			set
			{
				_coName = value;
			}
		}


		public SqlString CoEmail
		{
			get
			{
				return _coEmail;
			}
			set
			{
				_coEmail = value;
			}
		}


		public SqlString CoContent
		{
			get
			{
				return _coContent;
			}
			set
			{
				_coContent = value;
			}
		}


		public SqlString NID
		{
			get
			{
				return _nID;
			}
			set
			{
				_nID = value;
			}
		}
		public SqlString NIDOld
		{
			get
			{
				return _nIDOld;
			}
			set
			{
				_nIDOld = value;
			}
		}


		public SqlBoolean CoStatus
		{
			get
			{
				return _coStatus;
			}
			set
			{
				_coStatus = value;
			}
		}
		#endregion
	}
}
